#!/usr/bin/python
# -*- coding: utf-8 -*-

from uliweb.utils.common import import_attr
from uliweb.orm import get_model
from uliweb import request

def menu():

    #保存菜单内容
    menu_items = []
    
    #获取登录用id
    user = request.user
    
    #print "user===>",user
    
    menumodel = get_model('menu')
    menurolemodel = get_model('menurole')
    
    #管理员全部菜单
    if user.is_superuser:
        #获取一级菜单
        ormdata = menumodel.filter(menumodel.c.menupid == 0).filter(menumodel.c.menutype == 1).order_by(menumodel.c.menuseq)
        
        for i in ormdata:
            menu_dict = {}
            
            _id = i.id
            menu_dict['name'] = i.menuname
            menu_dict['url'] = i.menuurl
            menu_dict['icon'] = i.menuicon
            
            
            #获取二级菜单
            ormdata_sub_count = menumodel.filter(menumodel.c.menupid == _id).filter(menumodel.c.menutype == 1).count()
            ormdata_sub = menumodel.filter(menumodel.c.menupid == _id).filter(menumodel.c.menutype == 1)
            
            if ormdata_sub_count != 0:
                menu_dict['subs'] = []
                for si in ormdata_sub:
                    menu_dict['subs'].append({'name':si.menuname,
                                              'url':si.menuurl,
                                              'icon':si.menuicon
                    })
    
            menu_items.append(menu_dict)
    else:
        menu_ids = []
        
        roledata = menurolemodel.all()
        
        #得到用户的菜单权限id
        for i in roledata:
            #如果权限中有当前用户存在
            if i.users.has(user):
                #得到当前用户的菜单id
                menu_ids = i.menus.ids()

                break
            
        #登录用户没有任何权限
        if len(menu_ids) == 0:
            norole_menu = '<li><a href="#"><i class="fa fa-ban"></i><span class="nav-label">没有任何权限</span></a></li>'
            return norole_menu
        
        
        m = []
        for i in menu_ids:
            mid = menumodel.get(i)
            if mid.menupid != 0 and mid.menupid not in m:
                m.append(mid.menupid)
                
        
        m = menu_ids.extend(m)

        #获取一级菜单
        ormdata = menumodel.filter(menumodel.c.id.in_(menu_ids)).filter(menumodel.c.menutype == 1).filter(menumodel.c.menupid == 0)
   
        for i in ormdata:
            menu_dict = {}
            
            _id = i.id
            menu_dict['name'] = i.menuname
            menu_dict['url'] = i.menuurl
            menu_dict['icon'] = i.menuicon
            
            
            #获取二级菜单
            ormdata_sub_count = menumodel.filter(menumodel.c.menupid == _id).filter(menumodel.c.menutype == 1).filter(menumodel.c.id.in_(menu_ids)).count()
            ormdata_sub = menumodel.filter(menumodel.c.menupid == _id).filter(menumodel.c.menutype == 1).filter(menumodel.c.id.in_(menu_ids))
            
            if ormdata_sub_count != 0:
                menu_dict['subs'] = []
                for si in ormdata_sub:
                    menu_dict['subs'].append({'name':si.menuname,
                                              'url':si.menuurl,
                                              'icon':si.menuicon
                    })
    
            menu_items.append(menu_dict)

    
    #menu_items = [{'name': 'main',
                    #'url':'/',
                    #'icon':'icon-dashboard'
                    #},{'name': 'osadmin',
                      #'url':'#',
                      #'icon':'icon-desktop',
                      #'subs':[
                          #{'name' : 'user' , 'title' : '用户管理' , 'url' : '/user/user' , 'icon' : 'icon-double-angle-right' ,},
                          #{'name' : 'menu' , 'title' : '菜单管理' , 'url' : '/menu/menu' , 'icon' : 'icon-double-angle-right' ,},
                          #{'name' : 'menurole' , 'title' : '权限管理' , 'url' : '/myrbac/menurole' , 'icon' : 'icon-double-angle-right' ,},
                          #]
                    #},{'name': 'devadmin',
                      #'url':'#',
                      #'icon':'icon-gift',
                      #'subs':[
                          #{'name': 'server', 'title':'服务器信息', 'url':'/idcdev/server','icon':'icon-double-angle-right',},
                          #]
                    #},
                  #]

    """
    html格式
    <li>
        <a href="#">
            <i class="icon-cog"></i>
            <span class="nav-label">系统管理</span>
            <span class="fa arrow"></span>
        </a>
        <ul class="nav nav-second-level">
            <li>
                <a class="J_menuItem" href="/myuser/user"><i class="fa fa-users"></i>用户管理</a>
            </li>
            <li>
                <a class="J_menuItem" href="/myuser/usergroup">用户组管理</a>
            </li>
        </ul>
    </li>
    """

    s = []
    for x in menu_items:
        
        subs = x.get('subs')
        if subs == None:
            s.append('<li><a href="%s"><i class="%s"></i><span class="nav-label">%s</span></a></li>' % (x['url'],x['icon'],x['name']))
        else:
            #展开子菜单
            s.append('<li><a href="%s" ><i class="%s"></i><span class="nav-label">%s</span><span class="fa arrow"></span></a><ul class="nav nav-second-level">' % (x['url'],x['icon'],x['name']))
            #获取子项菜单
            for j in x['subs']:
                s.append('<li><a class="J_menuItem" href="%s"><i class="%s"></i>%s</a></li>' % (j['url'],j['icon'],j['name']))

            s.append('</ul></li>')
            
    menu_list = ''.join(s)
    
    return menu_list

